<template>
  <!-- 角色管理 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="dataList" @query="getDataList(true)" @reset="refresh">
        <template v-slot:row></template>
        <template v-slot:actions>
          <el-button type="primary" v-permission="'role.add'" size="mini" icon="el-icon-plus" @click="(isShowDetails = true), (checkedItem = null)">新增</el-button>
          <el-button type="info" size="mini" icon="el-icon-refresh" @click="getDataList">刷新</el-button>
        </template>
        <template>
          <el-table-column align="center" type="index" width="50">
            <template slot-scope="scope">
              <el-radio v-model="scope.row.isChecked" :label="true" @change="handleCurrentChange(scope.row)">{{""}}</el-radio>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" prop="id" label="ID" width="80" /> -->
          <el-table-column prop="admin_id" label="创建者" align="center" width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ $public.accountLabel(scope.row.admin_id) }}<span v-show="scope.row.admin_id">【{{scope.row.admin_id}}】</span></span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="role_name" label="角色名称" width="300" />
          <el-table-column align="center" prop="remark" label="角色描述" />
          <el-table-column align="center" prop="created_at" label="创建日期" width="200" />
          <el-table-column align="left" prop="operation" label="操作" width="260">
            <template slot-scope="scope">
              <el-tooltip effect="dark" v-permission="'role.edit'" content="编辑" placement="top-start">
                <el-button size="mini" type="warning" @click.stop="editItem(scope.row)"><i class="el-icon-edit-outline" /></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="'role.delete'" content="删除" placement="top-start">
                <el-button size="mini" type="danger" @click.stop="deleteItem(scope.row)"><i class="el-icon-delete" /></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="'role.menu'" content="设置菜单权限" placement="top-start">
                <el-button size="mini" type="primary" @click.stop="
                    (isShowMenuDetails = true), (checkedItem = scope.row)
                  "><i class="el-icon-s-operation" /></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="'role.api'" content="设置api权限" placement="top-start">
                <el-button size="mini" type="primary" @click.stop="
                    (isShowApiDetails = true), (checkedItem = scope.row)
                  "><i class="el-icon-s-data" /></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </template>
        <template v-slot:footer>
          <pagination  :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getDataList" />
        </template>
      </search-table>
      <page-details :show="isShowDetails" @onHideDetails="onHideDetails" :data="checkedItem"></page-details>
      <menu-details :show="isShowMenuDetails" @onHideDetails="onHideDetails" :data="checkedItem"></menu-details>
      <api-details :show="isShowApiDetails" @onHideDetails="onHideDetails" :data="checkedItem"></api-details>
    </div>
  </div>
</template>

<script>
import {
  roleList,
  roleDel
} from '@/api/system.js'
import { throttle } from '@/utils/index'
import PageDetails from './page-details.vue'
import ApiDetails from './api-details.vue'
import MenuDetails from './menu-details.vue'
import SearchTable from '@/components/SearchTable'
export default {
  name: 'role',
  components: {
    PageDetails,
    ApiDetails,
    MenuDetails,
    SearchTable
  },
  data () {
    return {
      dataList: [],
      checkedItem: null,
      listLoading: false,
      isShowDetails: false,
      isShowApiDetails: false,
      isShowMenuDetails: false,
      inputVal: '',
      currentTotal: 0,
      listQuery: {
        page: 1,
        size: 50,
      },
    }
  },
  created () {
    // this.getDataList()
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取角色列表
    async getDataList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.dataList = [];
      that.checkedItem = null;
      that.listLoading = true;
      const response = await roleList(params)
      if (response.code==1) {
        if (response.data.list) {
          response.data.list.forEach(item => {
            item.operation = ''
            item.isChecked = false
          })
          that.currentTotal = response.data.total
          that.dataList = response.data.list
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false
    },
    // 编辑
    editItem (row) {
      let that = this;
      that.isShowDetails = true
      that.checkedItem = row
      that.handleCurrentChange(row)
    },
    // 删除
    deleteItem (row) {
      let that = this;
      that.handleCurrentChange(row)
      that.$confirm(`是否删除【${row.role_name}】角色`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        roleDel({
          id: row.id
        }).then(function (res) {
          that.getDataList()
          that.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      }).catch(() => { });
    },
    // 选择表格数据
    handleCurrentChange (val) {
      this.checkedItem = val;
      this.dataList.map(item => {
        if (item.id === val.id) {
          item.isChecked = true
        } else {
          item.isChecked = false
        }
      })
    },
    // 隐藏详情
    onHideDetails (show) {
      this.isShowDetails = false
      this.isShowMenuDetails = false
      this.isShowApiDetails = false
      if (show) {
        this.getDataList()
      }
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
      }
      this.getDataList('');
    }, 2000),
  }
}
</script>

<style lang="scss" scoped>
</style>
